<?php
/**
 * Created by PhpStorm.
 * User: 牛赞赞
 * Date: 2021/4/19
 * Time: 11:25
 */

namespace App\Service;


use App\Model\GameLevel;

class GrandMatchService
{
    const GAME_TYPE = 1; // 游戏类别：大奖赛
    const GAME_ID = 6; // 游戏类别：大奖赛

    // 查询指定对局的奖品
    public static function getRoundPrize($round_num)
    {
        $where = [
            ['game_level.type', '=', self::GAME_TYPE],
            ['game_level.level', '=', $round_num],
            ['game_level.game_id', '=', self::GAME_ID]
        ];
        $records = GameLevel::getRecords($where, 'chance_per');
        $prize = [];
        if ($records['code'] == 0) {
            $prize = $records['data'];
        }

        return $prize;
    }

    // 获取下一阶段奖品
    public static function getNextStagePrize($curr_round)
    {
        $where = [
            ['game_level.type', '=', self::GAME_TYPE],
            ['game_level.game_id', '=', self::GAME_ID],
        ];
        $stage = [];
        $records = GameLevel::getRecords($where, 'level', 'asc');
        if (!$records['code']) {
            for ($i = $curr_round + 1; $i <= 9; $i++) {
                $flag = false;
                foreach ($records['data'] as $k => $v) {
                    if ($v->level == $i) {
                        $stage = $i;
                        $flag = true;
                        break;
                    }
                }
                if ($flag) {
                    break;
                }
            }
        }
        return self::getRoundPrize($stage);
    }

    // 获取上一阶段奖品
    public static function getPreStagePrize($curr_round)
    {
        $where = [
            ['game_level.type', '=', self::GAME_TYPE],
            ['game_level.game_id', '=', self::GAME_ID],
        ];
        $stage = [];
        $records = GameLevel::getRecords($where, 'level', 'asc');
        if (($curr_round > 1) && ($records['code'] == 0)) {
            for ($i = $curr_round - 1; $i > 0; $i--) {
                $flag = false;
                foreach ($records['data'] as $k => $v) {
                    if ($v->level == $i) {
                        $stage = $i;
                        $flag = true;
                        break;
                    }
                }
                if ($flag) {
                    break;
                }
            }
        }
        return self::getRoundPrize($stage);
    }

    // 抽奖概率算法
    private static function get_rand($proArr)
    {
        $result = '';
        //概率数组的总概率精度
        $proSum = array_sum($proArr);
        //概率数组循环
        foreach ($proArr as $key => $proCur) {
            $randNum = mt_rand(1, $proSum);
            if ($randNum <= $proCur) {
                $result = $key;
                break;
            } else {
                $proSum -= $proCur;
            }
        }
        unset ($proArr);
        return $result;
    }

    public static function showPrize($arr)
    {
        $prize = [];
        if (!empty($arr)) {
            foreach ($arr as $kks => $vvs) {
                $prize[] = [
                    'cpns_id' => $vvs->cpns_id,
                    'cpns_expire' => $vvs->cpns_expire,
                    'cpns_name' => $vvs->cpns_name,
                    'show_pre' => $vvs->show_pre,
                    'show_apply' => $vvs->show_apply,
                    'stage' => (int)$vvs->level
                ];
            }
        }
        return $prize;
    }
}